<?php
namespace Admin\Controller;
class QuestionController extends BaseController {
    public function __construct($check_login = true) {
        parent::__construct($check_login);
    }

    public function index() {
        $this->display();
    }

    public function getQuestionList() {
        //post请求，获取 pageNo 和 pageSize
        if (!isWebLogin()) {
            fail_login('请重新登录');
        }
        if (!$_POST) {
            fail("需要post请求");
        }
        $data = get_auto_data($_REQUEST);
        $pageNo = $data['pageNo'];
        $pageSize = $data['pageSize'];
        $goods_state = $data['goods_state'];
        if (!is_int($pageNo) || $pageNo < 0) {
            fail("分页参数错误");
            return;
        }
        if (!is_int($pageSize) || $pageSize < 1) {
            fail("分页参数错误");
            return;
        }
        $where = array();
        if ($goods_state) {
            $where = array(
                'goods_state' => $goods_state
            );
        }
        $m_goods = M('goods');
        $total_count = $m_goods->where(array(
            'goods_state' => 1
        ))->count();
        $goods_list = $m_goods->field('
        unique_id,
        goods_sn,
        goods_name,
        head_imgs,
        origin_price,
        current_price,
        collage_start_count,
        collage_price_header,
        collage_price_member,
        collage_price_back_to_header,
        collaged_count_forlook,
        goods_desc,
        goods_state,
        dead_time,
        goods_state,
        shop_name,
        shop_logo,
        shop_keyword,
        shop_address,
        shop_linkman,
        shop_contact_num,
        goods_share_msg
        ')->where($where)->order('create_time desc')->limit($pageNo * $pageSize, $pageSize)->select();
        if (sizeof($goods_list) > 0) {
            $m_goods_order = M('goods_order');
            foreach ($goods_list as $key => $value) {
                //查询订单表，查看已支付该商品的订单数量
                $order_count = $m_goods_order->where(array(
                    'goods_id'  => $value['unique_id'],
                    'is_pay'    => 1,
                    'is_cancel' => 0
                ))->count();
                $value['order_count'] = $order_count;
                $value['deadTimeStr'] = date('Y-m-d', $value['dead_time']);
                $value['goods_desc'] = htmlspecialchars_decode($value['goods_desc']);
                $goods_list[$key] = $value;
            }
            succeed(array(
                'goods_list'  => $goods_list,
                'total_count' => $total_count
            ), '获取商品列表成功');
        } else {
            succeed(array(
                'goods_list'  => array(),
                'total_count' => 0
            ), '获取商品列表成功');
        }
    }

    public function statistic() {
        $m_goods = M('goods');
        $count_goodson = $m_goods->field('goods_state')->where(array(
            'goods_state' => 1
        ))->count();
        $count_goodsoff = $m_goods->field('goods_state')->where(array(
            'goods_state' => 2
        ))->count();
        succeed(array(
            'count_goodson'  => $count_goodson,
            'count_goodsoff' => $count_goodsoff,
        ));
    }
}